<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

use app\lib\baidu\Api as baiduApi;


class Plan extends Controller
{
    public function htsc(){
        $list = Db::name('htsc')->where('status',0)->select();
        foreach ($list as $k => $vo) {
            $result = (new baiduApi())->htscTask($vo['task_id']);
            if ($result['status'] == 0) {
                continue;
            }
            $msg = Db::name('msg')->where('id',$vo['msg_id'])->find();
            $save_data = [
                'rtime'=>time()
            ];
            $save_data['duration'] = $save_data['rtime'] - $msg['ctime'];

            if ($result['status'] == -1) {
                Db::name('htsc')->where('id',$vo['id'])->update(['status'=>-1,'content'=>$result['reason']]);
                $save_data['reply'] = $result['reason'];
            }elseif($result['status'] == 1){
                Db::name('htsc')->where('id',$vo['id'])->update(['status'=>1,'content'=>json_encode($result['data'])]);

                $data = $result['data'][0];
                $message = json_decode($msg['message'],true);
                $content = '<h6>关于《'.$message['name'].'》的审查结果</h6><div class="form-row"><table class="table-bordered">';
                $content .= '<tr><th>风险等级</th><th>相应条款的原文</th><th>审查结果</th></tr>';
                foreach ($data['chatContents'] as $cc_k => $cc_v) {
                    $cols_num = count($cc_v['tags']);
                    $content .= '<tr>';
                    $content .= '<td rowspan="'.$cols_num.'">'.$cc_v['riskName'].'</td>';

                    $content .= '<td>'.$cc_v['tags'][0]['value'].'</td>';

                    $predictResult = $cc_v['predictResult'];
                    $predictResult = str_replace(['```json','json```','```'], '', $predictResult);
                    $predictResultArr = json_decode($predictResult,true);
                    if (!empty($predictResultArr)) {
                        $predictResult = [];
                        foreach ($predictResultArr as $predictResult_k => $predictResult_v) {
                            $predictResult[] = $predictResult_k.'：'.$predictResult_v;
                        }
                        $predictResult = implode("\n",$predictResult);
                    }
                    
                    $content .= '<td rowspan="'.$cols_num.'">'.$predictResult.'</td>';
                    $content .= '</tr>';
                    foreach ($cc_v['tags'] as $t_k => $t_v) {
                        if ($t_k == 0) {
                            continue;
                        }
                        $content .= '<tr><td>'.$t_v['value'].'</td></tr>';
                    }

                }
                $content .= '</table></div>';

                $save_data['reply'] = $content;
            }

            Db::name('msg')->where('id',$vo['msg_id'])->update($save_data);
        }
        echo 'success';
    }

    public function aiDraw(){
        $list = Db::name('aidraw')->where('status',0)->select();

        $api = new baiduApi();
        $token = $api->token([
            'client_id'=>'nuCDgwPHzU3JSCZ9BaahYT44',
            'client_secret'=>'LlyWXJJJD3ew7vFC27kxBWwJspR6nSn3'
        ]);
        if (empty($token['access_token'])) {
            return json(['code'=>5000,'msg'=>'token获取失败']);
        }
        foreach ($list as $k => $vo) {
            $result = (new baiduApi())->aidrawTask($token['access_token'],$vo['task_id']);
            if ($result['status'] == 0) {
                continue;
            }
            $msg = Db::name('msg')->where('id',$vo['msg_id'])->find();
            $save_data = [
                'rtime'=>time()
            ];
            $save_data['duration'] = $save_data['rtime'] - $msg['ctime'];

            if ($result['status'] == -1) {
                Db::name('aidraw')->where('id',$vo['id'])->update(['status'=>-1,'content'=>$result['reason']]);
                $save_data['reply'] = $result['reason'];
            }elseif($result['status'] == 1){
                Db::name('aidraw')->where('id',$vo['id'])->update(['status'=>1,'content'=>json_encode($result['data'])]);

                $data = $result['data'];
                $content = [];
                foreach ($data as $image) {
                    if ($image['sub_task_status'] == 'SUCCESS') {
                        $content = array_merge($content,array_column($image['final_image_list'], 'img_url'));
                    }
                }
                $save_data['reply'] = json_encode($content);
            }

            Db::name('msg')->where('id',$vo['msg_id'])->update($save_data);
        }
        echo 'success';
    }

    public function finishActivity(){
        Db::name('wx_activity')->where([
            ['status','=',0],
            ['activity_date','<',date('Y-m-d')]
        ])->update(['status'=>1]);
    }




   
}
